<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="关键词" prop="keyWords">
        <el-input
          v-model="queryParams.keyWords"
          placeholder="请输入关键词"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">
          搜索
        </el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd">
          新增
        </el-button>
      </el-col>
    </el-row>

    <!-- 表格数据 -->
    <el-table v-loading="loading" :data="tableList">
      <el-table-column label="序号" type="index" width="155" align="center" />
      <el-table-column label="产品名称" prop="productName" width="120" />
      <el-table-column label="产品类型" prop="productType" width="120" />
      <el-table-column
        label="银行名称"
        prop="productBank"
        :show-overflow-tooltip="true"
        width="150"
      />
      <el-table-column
        label="产品额度"
        prop="productQuota"
        :show-overflow-tooltip="true"
        width="250"
      />
      <el-table-column label="适用特性" prop="productRule" width="100" />
      <!-- <el-table-column
                label="产品周期"
                align="center"
                prop="productExpire"
            >
                <template #default="scope">
                    <span>{{
                        parseTime(scope.row.productExpire, '{y}-{m}-{d}')
                    }}</span>
                </template>
            </el-table-column> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-tooltip content="编辑" placement="top">
            <el-button link type="primary" @click="handleUpdate(scope.row)">
              编辑
            </el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button link type="primary" @click="handleDelete(scope.row)">
              删除
            </el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或编辑产品配置对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="dialogRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="产品名称" prop="productName">
          <el-input v-model="form.productName" placeholder="请输入产品名称" />
        </el-form-item>
        <el-form-item label="产品类型" prop="productType">
          <el-input v-model="form.productType" placeholder="请输入产品类型" />
        </el-form-item>
        <el-form-item label="银行名称" prop="productBank">
          <el-input v-model="form.productBank" placeholder="请输入银行名称" />
        </el-form-item>
        <el-form-item label="产品额度" prop="productQuota">
          <el-input v-model="form.productQuota" placeholder="额度区间" />
        </el-form-item>
        <el-form-item label="适用特性" prop="productRule">
          <el-input
            v-model="form.productRule"
            placeholder="产品利率、适用企业等"
          />
        </el-form-item>
        <!-- <el-form-item label="产品周期" prop="roleSort">
          <el-date-picker
            v-model="form.productExpire"
            type="date"
            placeholder="请选择申报截止日期"
            :size="size"
          />
        </el-form-item> -->
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<style scoped>
>>> .el-date-editor--date {
  width: 100%;
}
</style>
<script setup name="Role">
import { addItem, deleteItem, getTableList, updateInfo } from '@/api/product'

const { proxy } = getCurrentInstance()
const tableList = ref([])
const open = ref(false)
const loading = ref(true)
const total = ref(0)
const title = ref('')
const dateRange = ref([])
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    keyWords: undefined,
  },
  rules: {
    productName: [
      { required: true, message: '产品名称不能为空', trigger: 'blur' },
    ],
    productType: [
      { required: true, message: '产品类型不能为空', trigger: 'blur' },
    ],
    productBank: [
      { required: true, message: '银行名称不能为空', trigger: 'blur' },
    ],
    productQuota: [
      { required: true, message: '产品额度不能为空', trigger: 'blur' },
    ],
    productExpire: [
      {
        type: 'date',
        required: false,
        message: '请选择日期类型',
        trigger: 'change',
      },
    ],
  },
})

const { queryParams, form, rules } = toRefs(data)

/** 查询产品列表 */
function getList() {
  loading.value = true
  getTableList(proxy.addDateRange(queryParams.value, dateRange.value)).then(
    (response) => {
      tableList.value = response.rows
      total.value = response.total
      loading.value = false
    }
  )
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}
/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = []
  proxy.resetForm('queryRef')
  handleQuery()
}
/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$modal
    .confirm('你确定要删除此产品吗？')
    .then(function () {
      return deleteItem(row.id)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess('删除成功')
    })
    .catch(() => {})
}
/** 重置新增的表单以及其他数据  */
function reset() {
  form.value = {
    id: undefined,
    productName: undefined,
    productBank: undefined,
    productQuota: undefined,
    productRule: undefined,
    productExpire: undefined,
    productType: undefined,
  }
  proxy.resetForm('dialogRef')
}
/** 添加产品 */
function handleAdd() {
  reset()
  open.value = true
  title.value = '新增产品'
}
/** 编辑产品 */
function handleUpdate(row) {
  reset()
  form.value = Object.assign({}, row)
  open.value = true
  title.value = '编辑产品'
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs['dialogRef'].validate((valid) => {
    if (valid) {
      if (form.value.id != undefined) {
        updateInfo(form.value).then((response) => {
          proxy.$modal.msgSuccess('编辑成功')
          open.value = false
          getList()
        })
      } else {
        addItem(form.value).then((response) => {
          proxy.$modal.msgSuccess('新增成功')
          open.value = false
          getList()
        })
      }
    }
  })
}
/** 取消按钮 */
function cancel() {
  open.value = false
  reset()
}
getList()
</script>
